package com.src.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;

import run.dbConnection;

import com.src.calendarItem.Event;

public class ViewEvent extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private Event event;
	private dbConnection dbconn;
	private EventView eventview;
	/**
	 * This is the default constructor
	 */
	public ViewEvent(Event ev) {
		super();
		this.event = ev;
		initialize();
		dbconn = new dbConnection();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 400);
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo(null);
		this.setTitle("View Event");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			
			eventview = new EventView(this.event);
			
			jContentPane.add(eventview,BorderLayout.CENTER);
			
			JButton okEvent = new JButton("OK");
			okEvent.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dispose();
				}
			});
			
			JButton editEvent = new JButton("Edit");
			editEvent.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					//update the event
					if(dbconn.updateEvent(eventview.getEvent()))
					{
						JOptionPane.showConfirmDialog(null, "Event Successful Updated!");
						dispose();
					}
					else
					{
						JOptionPane.showConfirmDialog(null, "Error Occurred", "Warning", JOptionPane.ERROR_MESSAGE);
					}
				}
			});
			
			JButton deleteEvent = new JButton("Delete"); 
			deleteEvent.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					//delete the event
					if(dbconn.deleteEvent(event.getID()))
					{
						JOptionPane.showConfirmDialog(null,"Event Successful Deleted!");
						dispose();
					}
					else
					{
						JOptionPane.showConfirmDialog(null, "Error: Delete Event Failed!", "Warning", JOptionPane.ERROR_MESSAGE);
					}
					
				}
			});
			
			JPanel controlPane = new JPanel(new FlowLayout());
			
			controlPane.add(okEvent);
			controlPane.add(editEvent);
			controlPane.add(deleteEvent);
			jContentPane.add(new JLabel(" "), BorderLayout.NORTH);
			jContentPane.add(new JLabel("  "), BorderLayout.EAST);
			jContentPane.add(controlPane, BorderLayout.SOUTH);
		}
		return jContentPane;
	}

}
